package com.baizhi.utils;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneUtil {
    private static FSDirectory directory;
    public static IKAnalyzer analyzer;
    private static DirectoryReader reader;
    private static Version version;

    static{
        try {
            directory=FSDirectory.open(new File("D://lucene"));
            version=Version.LUCENE_44;
            analyzer = new IKAnalyzer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取索引写出对象
     * @return
     */
    public static IndexWriter getIndexWriter(){
        IndexWriterConfig config = new IndexWriterConfig(version, analyzer);
        IndexWriter indexWriter=null;

        try {
            indexWriter = new IndexWriter(directory,config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexWriter;
    }

    /**
     * 获取索引查询对象
     * @return
     */
    public static IndexSearcher getIndexSearcher(){
        try {
            reader = DirectoryReader.open(directory);
        } catch (IOException e) {
            throw new RuntimeException("没有索引库数据");
        }
        return new IndexSearcher(reader);
    }

    /**
     * 提交   释放资源
     * @param indexWriter
     */
    public static void indexCommit(IndexWriter indexWriter){
        try {
            indexWriter.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            indexWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 回滚 释放资源
     * @param indexWriter
     */
    public static void indexRollback(IndexWriter indexWriter){
        try {
            indexWriter.rollback();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            indexWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 传入搜索词，获得Query对象
     * @param keyWord
     * @return
     */
    public static Query getQuery(String keyWord){
        String[] fields={"articleId","articleName","articleContent"};
        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(version, fields, analyzer);
        Query query=null;
        try {
            query = queryParser.parse(keyWord);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return query;
    }


    /**
     * 判断索引库中有没有索引文件 如果没有 返回false  如果有返回true
     * @return
     */
    public static boolean judgeIndexDB(){
        IndexSearcher indexSearcher = null;
        Query query = new MatchAllDocsQuery();
        TopDocs topDocs = null;
        try {
            indexSearcher = getIndexSearcher();
            topDocs = indexSearcher.search(query, 10000);
        } catch (Exception e) {
            return false;
        }
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        if (scoreDocs.length==0){
            return false;
        }
        return true;
    }

}
